package com.fare.taluo.core.domain.crawl;

import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;

import com.dayatang.domain.Entity;
import com.dayatang.domain.InstanceFactory;
import com.dayatang.domain.QuerySettings;
import com.fare.taluo.GlobalsConst;


/**
 * 抓取任务执行队列模板
 * <p>
 * 根据该模板生成<@link>TaskQueueEntity</link>
 * 
 * @author xiangsf 2013-2-17
 * 
 */
@javax.persistence.Table(name = "CRAWL_TASK_TEMPLATE")
@javax.persistence.Entity
public class TaskTemplateEntity extends
com.fare.taluo.base.ddd.domain.baseentity.BaseEntity{
	/**
	 * 
	 */
	private static final long serialVersionUID = -4448819408322145384L;
//	@Id
//	@GeneratedValue(strategy = GenerationType.AUTO, generator = FDC_SEQ_GEN)
//	@SequenceGenerator(name = FDC_SEQ_GEN, sequenceName = FDC_SEQUENCE)
//	@Column(name = "id")
//	private Long id;
	/**
	 * 任务名称
	 */
	@Column(name = "name")
	private String name;

	/**
	 * 起点城市区域码
	 */
	@Column(name = "area_code")
	private String areaCode;
	/**
	 * 起点城市区域名称
	 */
	@Column(name = "area_name")
	private String areaName;
	/**
	 * 起点城市
	 */
	@Column(name = "from_city")
	private String fromCity;

	@Column(name = "from_city_name")
	private String fromCityName;

	@Column(name = "to_city")
	private String toCity;

	@Column(name = "to_city_name")
	private String toCityName;

	@Column(name = "grab_channel_id")
	private Long grabChannelId;

	@Column(name = "grab_channel")
	private String grabChannel;

	/**
	 * 附加的hbase应用值，用于生成rowkey,以便于应用根据该值统计
	 * <p>实际上使用下面的"抓取渠道ID"也可，但基于应用已完成部分功能，补充上该值
	 */
	@Column(name = "attach_hbase_key")
	private String attachHbaseKey;
	
	@Column(name = "frequency")
	private int frequency;

	@Column(name = "next_grab_time")
	private Date nextGrabTime;

	@Column(name = "validity_to")
	private Date validityTo;

	@Column(name = "validity_from")
	private Date validityFrom;

	@Column(name = "status")
	private Integer status = GlobalsConst.DATA_STATUS_NORMAL;

	@Column(name = "priority")
	private int priority;

	@Column(name = "grab_scope_end")
	private int grabScopeEnd;

	@Column(name = "grab_scope_begin")
	private int grabScopeBegin;
	// 是否抓回程
	@Column(name = "is_return")
	private Integer isReturn = 0;

	@Column(name = "return_date_scope")
	private int returnDateScope;

	@Column(name = "user_id")
	private Long userId;

	@Column(name = "user_name")
	private String userName;
	
	@Column(name = "remark")
	private String remark;

	@Column(name = "create_time")
	private Date createTime = new Date();
	
	@OneToMany(cascade=CascadeType.ALL,fetch = FetchType.EAGER,mappedBy = "taskTemplateEntity")
	private Set<TaskAssignDayEntity> assignDayList;
	
	public TaskTemplateEntity(){}
	
	
	public TaskTemplateEntity(String areaCode, String areaName,
			String fromCity, String fromCityName, String toCity,
			String toCityName, Long graChannelId, String grabChannel,
			String attachHbaseKey) {
		super();
		this.areaCode = areaCode;
		this.areaName = areaName;
		this.fromCity = fromCity;
		this.fromCityName = fromCityName;
		this.toCity = toCity;
		this.toCityName = toCityName;
		this.grabChannelId = graChannelId;
		this.grabChannel = grabChannel;
		this.attachHbaseKey = attachHbaseKey;
	}
	
	

	


	public Set<TaskAssignDayEntity> getAssignDayList() {
		return assignDayList;
	}


	public void setAssignDayList(Set<TaskAssignDayEntity> assignDayList) {
		this.assignDayList = assignDayList;
	}


//	public Long getId() {
//		return id;
//	}
//
//	public void setId(Long id) {
//		this.id = id;
//	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getIsReturn() {
		return isReturn;
	}

	public void setIsReturn(Integer isReturn) {
		this.isReturn = isReturn;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getAreaCode() {
		return areaCode;
	}

	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}

	public String getAreaName() {
		return areaName;
	}

	public void setAreaName(String areaName) {
		this.areaName = areaName;
	}

	public Long getGrabChannelId() {
		return grabChannelId;
	}

	public void setGrabChannelId(Long grabChannelId) {
		this.grabChannelId = grabChannelId;
	}

	public String getFromCity() {
		return fromCity;
	}

	public void setFromCity(String fromCity) {
		this.fromCity = fromCity;
	}

	public String getFromCityName() {
		return fromCityName;
	}

	public void setFromCityName(String fromCityName) {
		this.fromCityName = fromCityName;
	}

	public String getToCity() {
		return toCity;
	}

	public void setToCity(String toCity) {
		this.toCity = toCity;
	}

	public String getToCityName() {
		return toCityName;
	}

	public void setToCityName(String toCityName) {
		this.toCityName = toCityName;
	}

	public String getGrabChannel() {
		return grabChannel;
	}

	public void setGrabChannel(String grabChannel) {
		this.grabChannel = grabChannel;
	}

	

	public int getFrequency() {
		return frequency;
	}


	public void setFrequency(int frequency) {
		this.frequency = frequency;
	}


	public Date getNextGrabTime() {
		return nextGrabTime;
	}

	public String getAttachHbaseKey() {
		return attachHbaseKey;
	}

	public void setAttachHbaseKey(String attachHbaseKey) {
		this.attachHbaseKey = attachHbaseKey;
	}

	public void setNextGrabTime(Date nextGrabTime) {
		this.nextGrabTime = nextGrabTime;
	}

	public Date getValidityTo() {
		return validityTo;
	}

	public void setValidityTo(Date validityTo) {
		this.validityTo = validityTo;
	}

	public Date getValidityFrom() {
		return validityFrom;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public void setValidityFrom(Date validityFrom) {
		this.validityFrom = validityFrom;
	}

	public int getPriority() {
		return priority;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

	public int getGrabScopeEnd() {
		return grabScopeEnd;
	}

	public void setGrabScopeEnd(int grabScopeEnd) {
		this.grabScopeEnd = grabScopeEnd;
	}

	public int getGrabScopeBegin() {
		return grabScopeBegin;
	}

	public void setGrabScopeBegin(int grabScopeBegin) {
		this.grabScopeBegin = grabScopeBegin;
	}


	public int getReturnDateScope() {
		return returnDateScope;
	}

	public void setReturnDateScope(int returnDateScope) {
		this.returnDateScope = returnDateScope;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
//private static TaskTemplateEntityRepository taskTemplateRepository = InstanceFactory.getInstance(TaskTemplateEntityRepository.class);
	private static TaskTemplateEntityRepository getTaskTemplateEntityRepository(){
		return InstanceFactory.getInstance(TaskTemplateEntityRepository.class);
	}
	
	public void remove() {
		getTaskTemplateEntityRepository().remove(this);
	}
	
	public void saveOrUpdateTaskInfoEntity() {
		getTaskTemplateEntityRepository().save(this);
	}
	
	public List<TaskTemplateEntity> findTaskInfoEntityCriteriaList(String grabChannel,
			String fromCity, String toCity, int grabsopeBegin,int grabsopeEnd, Date validityFrom,int pageNum,int pageSize){
		return getTaskTemplateEntityRepository().findTaskInfoEntityCriteriaList(grabChannel, fromCity, toCity, grabsopeBegin,grabsopeEnd,validityFrom, pageNum, pageSize);
	}
	
	/**
	 * 按条件查询任务的数量
	 * @param grabChannel抓取渠道
	 * @param fromCity   出发地
	 * @param toCity     目的地
	 * @param grabsope   航班天数
	 * @return
	 */
	public int countTaskInfoEntityCriteria(String grabChannel,
			String fromCity, String toCity,Date validityFrom, int grabsopeBegin,int grabsopeEnd){
		return getTaskTemplateEntityRepository().countTaskInfoEntityCriteria(grabChannel, fromCity, toCity,validityFrom, grabsopeBegin,grabsopeEnd);
	}
	
	/**
	 * 通过条件返回
	 * @param grabChannel抓取渠道
	 * @param fromCity   出发地
	 * @param toCity     目的地
	 * @param grabsope   航班天数
	 * @return
	 */
	public List<TaskTemplateEntity> findTaskInfoByConsdions(String grabChannel, String fromCity, String toCity, int grabsopeBegin,int grabsopeEnd){
		return getTaskTemplateEntityRepository().findTaskInfoByConsdions(grabChannel, fromCity, toCity, grabsopeBegin,grabsopeEnd);
	}
	
	public List<TaskTemplateEntity> findTaskInfoByConsdion(String grabChannelId, String fromCity, String toCity){
		return getTaskTemplateEntityRepository().findTaskInfoByConsdion(grabChannelId, fromCity, toCity);
	}
	
	
	public List<TaskTemplateEntity> findByChannelId(long grabChannelId){
		return getTaskTemplateEntityRepository().findByChannelId(grabChannelId);
	}
	
	public List<TaskTemplateEntity> findByHbaseKey(String hbaseKey){
		return getTaskTemplateEntityRepository().findByHbaseKey(hbaseKey);
	}
	/**
	 * 通过一定条件返回任务，当添加一个新任务之前通过此接口，不为null才可以添加新任务
	 * @param grabChannel 抓去渠道
	 * @param fromCity 出发城市findByChannelId
	 * @param toCity 到达城市
	 * @param validityFrom 抓取时间段
	 * @param validityTo 抓取时间段
	 * @param grabsopeBegin 日期范围 开始
	 * @param grabsopeEnd 日期范围结束
	 * @return
	 */
	
	
	/**
	 * 根据航空公司返回航线
	 * @param grabChannel
	 * @return
	 */
	public List<TaskTemplateEntity> findTaskInfoByGrabChannel(String grabChannel){
		return getTaskTemplateEntityRepository().findTaskInfoByGrabChannel(grabChannel);
	}
	
	
	public  TaskTemplateEntity findTaskInfoEntityVo(String grabChannel,
			String fromCity, String toCity, Date validityFrom,Date validityTo,int grabsopeBegin,int grabsopeEnd) {
		return getTaskTemplateEntityRepository().findTaskInfoEntityVo(grabChannel, fromCity, toCity, validityFrom, validityTo, grabsopeBegin, grabsopeEnd);
	}
	
	/**
	 * 根据航段和网站进行重复性检验
	 * @param fromCity
	 * @param toCity
	 * @param hbaseKey
	 * @return
	 */
	public boolean existTask(String fromCity,String toCity,String hbaseKey){
		return getTaskTemplateEntityRepository().existTask(fromCity, toCity, hbaseKey);
	}

}
